﻿using System;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace AdGroupMembersWebPart.WebParts.AdGroupMembersWebPart
{
    class SkupineEditor : EditorPart
    {
        TextBox ldap;
        RadioButtonList opcije;
        TextBox ou;
        CheckBox errors;
        CheckBox cssLinkCheckBox;
        TextBox cssLinkUrl;
        CheckBox displayUserDataInWebPart;
        CheckBox displayLyncStatus;
        RadioButtonList displayLyncStatusRadioButtonList;
        

        protected override void CreateChildControls()
        {
            this.Title = "Web part settings";
            this.Controls.Add(new Label()
                 {
                     Text = "<div style='float:left;'><img src='/_layouts/15/images/ADGroupMembersWebPart/xnet-feature-logo.png' /></div><div><h3 style='text-align:center'>Xnet Display AD Group Members</h3></div><hr/>"
                 });
            ldap = new TextBox() { ID = "ldapID" };
            this.Controls.Add(new Label()
            {
                Text = "<br/>Enter FQDN of your domain<br/>",
                AssociatedControlID = ldap.ID
            });
            this.Controls.Add(ldap);

            opcije = new RadioButtonList() { ID = "opcijeId" };
            ListItem item = new ListItem("Display site users");
            opcije.Items.Add(item);
            item = new ListItem("Display users and groups from specific OU");
            opcije.Items.Add(item);
            opcije.AutoPostBack = true;
            opcije.SelectedIndexChanged += opcije_SelectedIndexChanged;
            this.Controls.Add(new Label()
                {
                    Text = "<br/>Choose an option for displaying users and groups<br/>",
                    AssociatedControlID = opcije.ID
                });
            this.Controls.Add(opcije);

            ou = new TextBox() { ID = "ouId" };
            if (opcije.SelectedIndex == 1)
            {
                ou.Enabled = true;
            }
            else
            {
                ou.Enabled = false;
            }
            this.Controls.Add(new Label()
                {
                    Text = "<br/>Enter OU where you want to display users and groups from<br/><span style='font-style:Italic'>If you select this option, site users and groups will not be displayed</span><br/>",
                    AssociatedControlID = ou.ID
                });
            this.Controls.Add(ou);

            this.Controls.Add(new Label()
                {
                    Text = "<br/><span style='font-style:Italic'>Enter OU filter in following format: <strong>OU=Employees,DC=xnetdev,dc=si</strong></span><br/>If you leave OU filter blank, you will query whole domain<br/><hr/>"
                });


            cssLinkCheckBox = new CheckBox() { ID = "cssLinkCheckBoxId" };
            cssLinkCheckBox.AutoPostBack = true;
            cssLinkCheckBox.CheckedChanged += cssLinkCheckBox_CheckedChanged;
            this.Controls.Add(cssLinkCheckBox);

            cssLinkUrl = new TextBox() { ID = "cssLinkUrlId" };
            cssLinkUrl.Enabled = cssLinkCheckBox.Checked;
            cssLinkUrl.CausesValidation = true;
            //cssLinkUrl.AutoPostBack=true;

            this.Controls.Add(new Label()
            {
                Text = "Use custom CSS file for web part branding<br/>",
                AssociatedControlID = cssLinkUrl.ID
            });
            this.Controls.Add(cssLinkUrl);
            CustomValidator cssLinkUrlValidator = new CustomValidator();
            cssLinkUrlValidator.ValidateEmptyText = true;
            cssLinkUrlValidator.ServerValidate += cssLinkUrlValidator_ServerValidate;
            cssLinkUrlValidator.ControlToValidate = cssLinkUrl.ID;
            cssLinkUrlValidator.Text = "Please enter valid url to custom CSS file";
            cssLinkUrlValidator.ForeColor = System.Drawing.Color.Red;
            this.Controls.Add(cssLinkUrlValidator);

            this.Controls.Add(new Label()
            {
                Text = "<hr/>"
            });

            displayUserDataInWebPart = new CheckBox() { ID = "displayUserDatainWebPartId" };
            this.Controls.Add(displayUserDataInWebPart);
            this.Controls.Add(new Label()
            {
                Text = "Display user data in web part (email and phone)<br/>",
                AssociatedControlID = displayUserDataInWebPart.ID
            });

            displayLyncStatus = new CheckBox() { ID = "dispalyLyncStatusCheckBoxId" };
            displayLyncStatus.AutoPostBack = true;
            displayLyncStatus.CheckedChanged += displayLyncStatus_CheckedChanged;
            this.Controls.Add(displayLyncStatus);
            this.Controls.Add(new Label()
            {
                Text = "Display Lync status for users",
                AssociatedControlID = displayLyncStatus.ID
            });
           
            displayLyncStatusRadioButtonList = new RadioButtonList() { ID = "dispalyLyncStatusRadioButtonListId" };
            displayLyncStatusRadioButtonList.Enabled = displayLyncStatus.Checked;
            displayLyncStatusRadioButtonList.Items.Add(new ListItem("Use UPN to display status","UPN"));
            displayLyncStatusRadioButtonList.Items.Add(new ListItem("Use mail attribute to display status","mail"));
            displayLyncStatusRadioButtonList.Items.Add(new ListItem("Use proxyAdresses attribute to display status", "proxyAddress"));
            displayLyncStatusRadioButtonList.CausesValidation = true;
            this.Controls.Add(displayLyncStatusRadioButtonList);
            CustomValidator displayLyncStatusRadioButtonListValidator = new CustomValidator();
            displayLyncStatusRadioButtonListValidator.ServerValidate += displayLyncStatusRadioButtonListValidator_ServerValidate;
            displayLyncStatusRadioButtonListValidator.Text = "If display Lync status is selected, you must chose attribute";
            displayLyncStatusRadioButtonListValidator.ForeColor = System.Drawing.Color.Red;

            errors = new CheckBox() { ID = "errorsId" };
            this.Controls.Add(errors);
            this.Controls.Add(new Label()
                {
                    Text = "Display errors (debug mode)<br/>",
                    AssociatedControlID = errors.ID
                });


            base.CreateChildControls();
            this.ChildControlsCreated = true;
        }

        void displayLyncStatusRadioButtonListValidator_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (displayLyncStatus.Checked && displayLyncStatusRadioButtonList.SelectedIndex == -1)
            {
                args.IsValid = false;
            }
            else
            {
                args.IsValid = true;
            }
        }

        void displayLyncStatus_CheckedChanged(object sender, EventArgs e)
        {

            if (!displayLyncStatus.Checked)
            {
                displayLyncStatusRadioButtonList.SelectedIndex = -1;
            }
            displayLyncStatusRadioButtonList.Enabled = displayLyncStatus.Checked;

        }

        void cssLinkUrlValidator_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (cssLinkCheckBox.Checked)
            {
                if (!String.IsNullOrEmpty(args.Value) && args.Value.EndsWith(".css"))
                {
                    args.IsValid = true;
                }
                else
                {
                    args.IsValid = false;
                }
            }
            else
            {
                args.IsValid = true;
            }

        }

        void cssLinkCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (cssLinkCheckBox.Checked)
            {
                cssLinkUrl.Enabled = true;
            }
            else
            {
                cssLinkUrl.Enabled = false;
                cssLinkUrl.Text = "";
            }
        }

        void opcije_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (opcije.SelectedValue == "Display users and groups from specific OU")
            {
                ou.Enabled = true;
            }
            else
            {
                ou.Enabled = false;
                ou.Text = "";
            }
        }

        public override void SyncChanges()
        {
            EnsureChildControls();
            global::AdGroupMembersWebPart.WebParts.AdGroupMembersWebPart.AdGroupMembersWp webPart = this.WebPartToEdit as global::AdGroupMembersWebPart.WebParts.AdGroupMembersWebPart.AdGroupMembersWp;
            if (webPart != null)
            {
                ldap.Text = webPart.LdapQuery;
                opcije.SelectedValue = webPart.Opcije;
                ou.Text = webPart.Ou;
                errors.Checked = webPart.ShowErrors;
                cssLinkCheckBox.Checked = webPart.UseCustomCssFile;
                cssLinkUrl.Enabled = webPart.UseCustomCssFile;
                cssLinkUrl.Text = webPart.CustomCssFileUrl;
                displayUserDataInWebPart.Checked = webPart.DisplayUserDetails;
                displayLyncStatus.Checked = webPart.DisplayLyncStatus;
                displayLyncStatusRadioButtonList.Enabled = webPart.DisplayLyncStatus;
                displayLyncStatusRadioButtonList.SelectedValue = webPart.DisplayLyncStatusAttribute;
            }
        }
        public override bool ApplyChanges()
        {
            EnsureChildControls();
            global::AdGroupMembersWebPart.WebParts.AdGroupMembersWebPart.AdGroupMembersWp webPart = this.WebPartToEdit as global::AdGroupMembersWebPart.WebParts.AdGroupMembersWebPart.AdGroupMembersWp;
            if (Page.IsValid)
            {
                if (webPart != null)
                {
                    webPart.LdapQuery = ldap.Text;
                    webPart.Opcije = opcije.SelectedValue;
                    webPart.Ou = ou.Text;
                    webPart.ShowErrors = errors.Checked;
                    webPart.UseCustomCssFile = cssLinkCheckBox.Checked;
                    webPart.CustomCssFileUrl = cssLinkUrl.Text;
                    webPart.DisplayUserDetails = displayUserDataInWebPart.Checked;
                    webPart.DisplayLyncStatus = displayLyncStatus.Checked;
                    webPart.DisplayLyncStatusAttribute = displayLyncStatusRadioButtonList.SelectedValue;
                }
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
